/**
 * Copyright [2011] [Raymond Pankhurst]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */
package passwordapp;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author rpankhurst
 */
public class PasswordEntry implements Comparable {
    public String Description = "";
    public String Username = "";
    public String Password = "";
    public String Notes = "";
    
    public int compareTo(Object RHS) {
        PasswordEntry R = (PasswordEntry) RHS;
        if(R != null) {
            return Description.toUpperCase().compareTo(R.Description.toUpperCase());
        } else {
            throw new ClassCastException("Invalid comparison with PasswordEntry");
        }
    }
    public String toString() {
        return "[PasswordEntry for " + this.Description + "]";
    }
    public byte[] toBytes() {
        byte[] desc = Description.getBytes();
        byte[] user = Username.getBytes();
        byte[] pw = Password.getBytes();
        byte[] n = Notes.getBytes();
        
        byte[] res = new byte[desc.length + user.length + pw.length + n.length + 4];
        System.arraycopy(desc, 0, res, 0, desc.length);
        res[desc.length] = 0;
        System.arraycopy(user, 0, res, desc.length + 1, user.length);
        res[desc.length + 1 + user.length] = 0;
        System.arraycopy(pw, 0, res, desc.length + 1 + user.length + 1, pw.length);
        res[desc.length + 1 + user.length + 1 + pw.length] = 0;
        System.arraycopy(n, 0, res, desc.length + 1 + user.length + 1 + pw.length + 1, n.length);
        res[res.length - 1] = 0;
        return res;
    }
    public void fromBytes(byte[] src) {
        String[] res = new String[4];
        int idx = 0, start = 0;
        for(int i = 0; i < src.length && idx < 4; i++) {
            if(src[i] == 0) {
                res[idx] = new String(src, start, i - start);
                idx++;
                start = i + 1;
            }
        }
        Description = res[0];
        Username = res[1];
        Password = res[2];
        Notes = res[3];
    }
}
